<?php
namespace Home\Controller;
use Think\Controller;
class StoreController extends CommonController {
    //总店铺下的子店铺
    public function index(){
        $storeModel = M("Store");
        $member = M("member");
        $where['is_main'] = 0;
        $where['parent_id'] = $this->sessionInfo['user_id'];
        $count = $storeModel->where($where)->count();
        $page = I('p') ? I('p') : '1';
        $pageSize = I('pageSize') ? I('pageSize') : 15;
        $list = $storeModel->where($where)->page($page,$pageSize)->order("add_time desc")->select();
        if(!empty($list)){
            //昨日开始和结束时间
            $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
            $endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
            $yesterdayWhere['third_class'] = array("neq",0);


            //今天开始时间
            $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
            $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
            $todayWhere['third_class'] = array('neq',0);
            $where1['third_class'] = array('neq',0);

            foreach($list as $k=>$v){
                $yesterdayWhere['second_class'] = $v['store_id'];
                $todayWhere['second_class'] = $v['store_id'];
                $where1['second_class'] = $v['store_id'];
                $where_1['s_id'] = $v['store_id'];
                $where_main['_complex'] = array(
                    $where_1,
                    $where1,
                    '_logic' => 'or'
                );
                $where_main2['_complex'] = array(
                    $where_1,
                    $yesterdayWhere,
                    '_logic' => 'or'
                );
                $where_main1['_complex'] = array(
                    $where_1,
                    $todayWhere,
                    '_logic' => 'or'
                );

                $where_main1['reg_time'] = array("between",array($beginYesterday,$endYesterday));
                $where_main2['reg_time'] = array("between",array($beginToday,$endToday));


                $list[$k]['yesterday'] = $member->where($where_main1)->count();
                $list[$k]['today'] = $member->where($where_main2)->count();
                $list[$k]['total'] = $member->where($where_main)->count();
            }
        }
        $Page       = new \Think\Page($count,$pageSize);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->setConfig('header','条留言');
        $Page->setConfig('first','首页');
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('last','尾页');
        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%<p id="pageTips">第 '.I('p',1).' 页/共 %TOTAL_PAGE% 页 ( '.$pageSize.' 条/页 共 %TOTAL_ROW% 条信息)</p>');
        $show       = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('list',$list);
        $this->display();
    }


    //导出店铺推广注册人
    public function excel(){
        $store_id = I("get.store_id",0,'intval');
        if(!$store_id){
            $this->error('信息错误');
        }
        $member = M("member");
        $where['second_class'] = $store_id;
        $where['third_class'] = array('neq',0);
        $where['first_class'] = $this->sessionInfo['user_id'];

        $where_1['s_id'] = $store_id;
        $where_main2['_complex'] = array(
            $where_1,
            $where,
            '_logic' => 'or'
        );

        $list = $member->where($where_main2)->order("reg_time desc")->select();
        if(!empty($list)){
            foreach($list as $k=>$v){
                $list[$k]['third_name'] = $member->where(array('user_id'=>$v['referid']))->getField("user_name");
//                $list[$k]['second_name'] = $member->where(array('user_id'=>$v['referid']))->getField("user_name");
            }

            require '../sellerserver/ThinkPHP/Library/Vendor/PHPExcel/PHPExcel.php';
            $excel = new \PHPExcel();

            $objActSheet = $excel->getActiveSheet();
            $objActSheet->setCellValue('A1', '用户名');
            $objActSheet->setCellValue('B1', '手机号');
            $objActSheet->setCellValue('C1', '推广人');
            $objActSheet->setCellValue('D1', '所属店铺');
            $objActSheet->setCellValue('E1', '注册时间');

            foreach ($list as $k => $v){
                $k +=2;
                $time = date('Y-m-d H:i:s',$v['reg_time']);
                $objActSheet->setCellValue('A'.$k, $v['user_name']);
                $objActSheet->setCellValue('B'.$k, $v['phone_mob']);
                $objActSheet->setCellValue('C'.$k, $v['third_name']);
                $objActSheet->setCellValue('D'.$k, $v['s_name']);
                $objActSheet->setCellValue('E'.$k, $time);
            }
            $excel = \PHPExcel_IOFactory::createWriter($excel, 'Excel2007');  //Excel2007  兼容好
            ob_end_clean();   //解决乱码
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename=店铺推广详细' . date('Ymd') . '.xlsx');
            header('Cache-Control: max-age=0');
            $excel->save('php://output');
            exit;
        }else{
            $this->error('您所选的数据为空！');
        }
    }


}